package com.yuelao.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 相同下标下元素类型要一致，否则校验结果会出错（包装类型）
 * @author FLY
 * @version v1.0
 * @project yuelao-server
 * @date 2017/9/11 18:13
 * @修改记录 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 */
public class CheckDataUtil {
    private List<Object> listTarget;
    private List<Object> listCheck;

    private CheckDataUtil() {
        listTarget = new ArrayList<>();
        listCheck = new ArrayList<>();
    }

    public static CheckDataUtil instance() {
        CheckDataUtil checkData = new CheckDataUtil();
        return checkData;
    }

    /**
     * 添加目标数据
     *
     * @param obj
     */
    public CheckDataUtil setTarData(Object obj) {
        listTarget.add(obj);
        return this;
    }

    /**
     * 被校验数据
     *
     * @param obj
     */
    public CheckDataUtil setCheckData(Object obj) {
        listCheck.add(obj);
        return this;
    }

    /**
     * 返回校验结果
     *
     * @return
     */
    public boolean initCheck() {
        boolean init = true;
        for (int i = 0; i < listCheck.size(); i++) {
            Class clazz = listCheck.get(i).getClass();
            if (String.class == clazz) {
                init = listCheck.get(i).equals(listTarget.get(i)) ? true : false;
                if (!init)
                    return init;
            } else if (BigDecimal.class == clazz) {
                BigDecimal target = (BigDecimal) listTarget.get(i);
                BigDecimal check = (BigDecimal) listCheck.get(i);
                init = check.compareTo(target) == 0 ? true : false;
                if (!init)
                    return init;
            } else if (Long.class == clazz) {
                init = (long) listCheck.get(i) == (long) listTarget.get(i) ? true : false;
                if (!init)
                    return init;
            } else if (Integer.class == clazz) {
                init = (int) listCheck.get(i) == (int) listTarget.get(i) ? true : false;
                if (!init)
                    return init;
            } else if (Double.class == clazz) {
                init = ((Double) listCheck.get(i)).compareTo((Double) listTarget.get(i)) == 0 ? true : false;
                if (!init)
                    return init;
            }
        }
        return init;
    }
}
